package bamboo.drawingkid.views.components;

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Gauge;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.TextField;

import bamboo.drawingkid.Main;
import bamboo.drawingkid.helpers.FileHandler;
import bamboo.drawingkid.helpers.ImageLoader;
import bamboo.drawingkid.helpers.Rectangle;
import bamboo.drawingkid.helpers.SaveFileListener;

import com.nokia.mid.ui.TextEditor;
import com.nokia.mid.ui.TextEditorListener;

/**
 *
 * @author tkor
 */
public class FileSaveDialog implements TextEditorListener, ButtonListener, SaveFileListener {

    private TextEditor texteditor;
    private boolean isShown = false;
    protected OKButton okbutton;
    protected CancelButton cancelbutton;
    protected CharEraserButton chareraserbutton;
    private Rectangle dialogArea;
    private Image background;
    private int x = 0;
    private int y = 160;
    private Graphics bg;
    private boolean showEraser = false;

    private Alert progress;
    
    public FileSaveDialog(Canvas parent) {
        ImageLoader loader = ImageLoader.getInstance();
        background = loader.loadImage("/dialog.png");
        
        int width = parent.getWidth();
        if(width > 240) {
            x = (width - background.getWidth())/2;
        }
        Font font = Font.getDefaultFont();
        texteditor = TextEditor.createTextEditor("", 50, TextField.ANY, 200, font.getHeight());
        texteditor.setParent(parent);
        texteditor.setPosition(x+20,y+60);
        texteditor.setBackgroundColor(0xFFFFFFFF);
        texteditor.setForegroundColor(0xFF000000);
        texteditor.setConstraints(TextField.NON_PREDICTIVE);
        texteditor.setTextEditorListener(this);
        
        dialogArea = new Rectangle(x, y, background.getWidth(), background.getHeight());
        okbutton = new OKButton();
        okbutton.addListener(this);
        cancelbutton = new CancelButton();
        cancelbutton.addListener(this);
        okbutton.setPosition(x+2,y+122);
        cancelbutton.setPosition(x+122, y+122);
        chareraserbutton = new CharEraserButton(texteditor);
        chareraserbutton.setPosition(x+122,y+122);
        
        progress = new Alert("Save file to memory", "Saving file . . .", null, AlertType.INFO);
        progress.setTimeout(3600000);
        Gauge g = new Gauge(null, false, Gauge.INDEFINITE, Gauge.CONTINUOUS_RUNNING);
        progress.setIndicator(g);
    }

    public void render(Graphics g) {
        if (isShown) {
            g.drawImage(background, x, y, Graphics.LEFT | Graphics.TOP);
            okbutton.render(g);
            if(!showEraser) {
                cancelbutton.render(g);
            }else {
                chareraserbutton.render(g);
            }
            g.setColor(0x000000);
            g.drawString("Enter a name:", x+20 ,y+35, Graphics.LEFT|Graphics.BOTTOM);
        }
    }

    public void hide() {
        isShown = false;
        texteditor.setVisible(false);
    }

    public void show() {
        isShown = true;
        texteditor.setVisible(true);
        texteditor.setFocus(true);
    }

    public boolean isVisible() {
        return isShown;
    }

    public void pointerPressed(int x, int y) {
        okbutton.pressed(x, y);
        if(showEraser) {
            chareraserbutton.pressed(x, y);
        }else {
            cancelbutton.pressed(x, y);
        }
    }

    public void pointerUnpressed(int x, int y) {
        if (!dialogArea.isPointInside(x, y)) {
            isShown = false;
            texteditor.setVisible(false);
            Main.getInstance().getDrawArea().allowDrawing(true);
        } else {
            okbutton.unpressed(x, y);
            if(showEraser) {
                chareraserbutton.unpressed(x, y);
            }else {
                cancelbutton.unpressed(x, y);
            }
        }
    }

    public void inputAction(TextEditor te, int actions) {
        
        if (texteditor.getContent().length() > 0) {
            showEraser = true;
        } else {
            showEraser = false;
        }

    }

    public void buttonPressed(Button caller) {
        if (caller.equals(okbutton)) {
            saveToFile();
        }
        //both buttons will hide the dialog
        isShown = false;
        texteditor.setVisible(false);
        Main.getInstance().getDrawArea().allowDrawing(true);
    }

    private void saveToFile() {
        final Image saveable = Main.getInstance().getDrawArea().getDrawing();
        FileHandler.saveImageToFile(texteditor.getContent(), saveable, this);
    }

	public void setBg(Graphics bg) {
		this.bg = bg;
	}

	public Graphics getBg() {
		return bg;
	}

	public void startSaveFile() {
		Main.getInstance().getDisplay().setCurrent(progress);
	}

	public void saveFileInProgress(int percent) {
		//Do nothing
	}

	public void saveFileError() {
		Main.getInstance().showError("Unable to save file!");
	}

	public void finishSaveFile() {
		// TODO Auto-generated method stub
		Main.getInstance().showDrawing();
	}
	
}
